package com.hxgis.statisticanalysis.intelligentIndicator.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxgis.statisticanalysis.intelligentIndicator.mapper.WorkOrderTriggerEventMapper;
import com.hxgis.statisticanalysis.intelligentIndicator.pojo.WorkOrderTriggerEvent;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author 赵恒
 * @date 2022/12/19
 */
@Service
public class WorkOrderTriggerEventService extends ServiceImpl<WorkOrderTriggerEventMapper, WorkOrderTriggerEvent> {
    @Resource
    WorkOrderTriggerEventMapper workOrderTriggerEventMapper;

    /**
     * 从数据库获取工单完成的时间
     * @param workOrderId
     * @return
     */
    public Date getCompletedTime(String workOrderId) {
        QueryWrapper<WorkOrderTriggerEvent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ticket_id", workOrderId);
        List<WorkOrderTriggerEvent> triggerEvents = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(triggerEvents)) {
            return null;
        }
        // 获取集合中已完成或已关闭的工单
        Optional<WorkOrderTriggerEvent> eventOptional = triggerEvents.stream().filter(workOrderTriggerEvent -> {
            if (workOrderTriggerEvent.getStatus().equals("已完成") || workOrderTriggerEvent.getStatus().equals("已关闭")) {
                return true;
            } else {
                return false;
            }
        }).findAny();
        if (!eventOptional.isPresent()) {
            return null;
        }
        WorkOrderTriggerEvent workOrderTriggerEvent = eventOptional.get();
        Date insertTime = workOrderTriggerEvent.getInsertTime();

        return insertTime;
    }
}
